/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.insurance.cis.daos;

import com.insurance.cis.connection.CisConnection;
import com.insurance.cis.models.Employee;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author TruongLQ
 */
public class EmployeeDao {
    
    public Employee getEmployee(String username, String password) {
        try {
            final String SQL = "select * from Employee where Username=? and Password=?";
            PreparedStatement pst= CisConnection.getInstance().getSqlConnection().prepareStatement(SQL);
            pst.setString(1, username);
            pst.setString(2, password);
            ResultSet rs = pst.executeQuery();
            if (rs.next()) {
                Employee employee = new Employee(
                        rs.getString(1), 
                        rs.getString(2), 
                        rs.getString(4),
                        rs.getString(5),
                        rs.getDate(6),
                        rs.getInt(7));
                return employee;
            }
            return null;
        } catch (SQLException ex) {
            Logger.getLogger(EmployeeDao.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
    
    public boolean changePassword(String username, String newPassword) {
        try {
            final String SQL = "update Employee set Password=? where Username=?";
            PreparedStatement pst= CisConnection.getInstance().getSqlConnection().prepareStatement(SQL);
            pst.setString(1, newPassword);
            pst.setString(2, username);
            pst.executeUpdate();
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(EmployeeDao.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }
}
